#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
class Solution {
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        int m = image.size();
        int n = image[0].size();
        int prev = image[sr][sc];
        if (prev == color)
        {
            return image;
        }
        queue<pair<int, int>> as;
        as.push({ sr,sc });
        while (as.size())
        {
            auto [a, b] = as.front();
            as.pop();
            image[a][b] = color;
            for (int i = 0; i < 4; i++)
            {
                int x = a + dx[i];
                int y = b + dy[i];
                if (x >= 0 && x < m && y >= 0 && y < n && image[x][y] == prev && image[x][y] != color)
                {
                    as.push({ x,y });
                }
            }
        }
        return image;
    }
};